<?php
	
	//Extract Path of eCMS installation, by extracting the paht of this file
	$pathi = pathinfo(__FILE__); 
	define("eCMS_DIR", $pathi['dirname']);
	
	/* Include alle needed Files */
	/* ========================= */
	
	/*
	//Define Array of all incluede files for eCMS
	$incFiles = array(
		'app/system/Runtime.class.php',
		'app/system/Async.class.php',
		
		'app/system/Utils.class.php',
		'app/system/Logger.class.php',
		
		'app/system/MySQLConnection.class.php',
		'app/lib/SafeSQL/SafeSQL.class.php',
		'app/db/DBList.class.php',
		'app/db/DBListItem.class.php',
		'app/system/TableAdapter.class.php', //Deprecated
		
		'app/system/SmartyPHPMailer.class.php',
		
		'app/lib/ImageTexter/ImageTexter.class.php',
		'app/lib/MultiSorter.class.php',
		
		'app/page/ErrorPage.class.php',
		'app/page/PageRequest.class.php',
		'app/page/PageFactory.class.php',
		'app/page/SiteFactory.class.php',
		'app/security/SecurityUser.class.php',
		
		'app/lib/smarty/Smarty.class.php',
		'app/page/SmartyObjectArrayWrapper.php',
	);
	
	foreach ($incFiles as $f) {
		require_once eCMS_DIR . "/" . $f;;
	}
	*/
	
	require_once eCMS_DIR . '/app/system/Runtime.class.php';
	require_once eCMS_DIR . '/app/system/eCMSException.class.php';
	require_once eCMS_DIR . '/app/system/Async.class.php';
	
	require_once eCMS_DIR . '/app/system/Utils.class.php';
	require_once eCMS_DIR . '/app/system/Logger.class.php';
	
	require_once eCMS_DIR . '/app/system/MySQLConnection.class.php';
	require_once eCMS_DIR . '/app/lib/SafeSQL/SafeSQL.class.php';
	require_once eCMS_DIR . '/app/db/DBList.class.php';
	require_once eCMS_DIR . '/app/db/DBListItem.class.php';
	require_once eCMS_DIR . '/app/db/DataMapper.class.php';
	
	/* Modules Section */
	require_once eCMS_DIR . '/app/module/ModuleInstanceList.class.php';
	require_once eCMS_DIR . '/app/module/ModuleList.class.php';
	require_once eCMS_DIR . '/app/module/ModuleManager.class.php';
	require_once eCMS_DIR . '/app/module/eCMSModule.class.php';
	require_once eCMS_DIR . '/app/module/eCMSModuleController.class.php';
	require_once eCMS_DIR . '/app/module/eCMSFileModuleController.class.php';
	
	/* Page-Related Section */
	require_once eCMS_DIR . '/app/page/PageList.class.php';
	require_once eCMS_DIR . '/app/page/AreaList.class.php';
	require_once eCMS_DIR . '/app/page/DesignList.class.php';
	require_once eCMS_DIR . '/app/page/PageLayoutList.class.php';
	require_once eCMS_DIR . '/app/page/PageRequest.class.php';
	require_once eCMS_DIR . '/app/page/PageFactory.class.php';
	require_once eCMS_DIR . '/app/page/SiteFactory.class.php';
	require_once eCMS_DIR . '/app/page/ErrorPage.class.php';
	
	/* DB Related */
	require_once eCMS_DIR . '/app/system/TableAdapter.class.php'; //Deprecated
	
	include_once eCMS_DIR . '/app/system/SmartyPHPMailer.class.php';
	require_once eCMS_DIR . '/app/lib/ImageTexter/ImageTexter.class.php';
	
	require_once eCMS_DIR . '/app/lib/MultiSorter.class.php';
	require_once eCMS_DIR . '/app/lib/UploadHandler/UploadHandler.class.php';
	
	require_once eCMS_DIR . '/app/security/SecurityUser.class.php';
	
	include_once eCMS_DIR . '/app/lib/smarty/Smarty.class.php';
	include_once eCMS_DIR . '/app/page/SmartyObjectArrayWrapper.php';

	class eCMS {
		
		private $conf;
		
		function __construct($config=null) {
			if($config) { $this->loadConfig($config); }	
		}
		
		function loadConfig($c) {
			$this->conf = $c;
			
			if(!isset($this->conf['JsLibsURL'])) { $this->conf['JsLibsURL'] = "content/libs"; }
			if(!isset($this->conf['sharedDesignURL'])) { $this->conf['sharedDesignURL'] = "content/design"; }
			if(!isset($this->conf['defaultPage'])) { $this->conf['defaultPage'] = "home"; }
			
		}
		
		function run() {
			
			if(!$this->conf) {
				user_error("No configuration specified! Exiting.");
				die();
			}
			
			if (isset($_POST["PHPSESSID"]))
			{
				session_id($_POST["PHPSESSID"]);
			}
			
			session_start();
			
			
			//Set Configuration to Runtime
			Runtime::setConfiguration($this->conf);
			
			
			/* Connect to SQL-Database */
			/* ======================= */
			
			$conn = MySQLConnection::getInstance();
			if(!$conn->testConnection()) {
				//Error Message;
				trigger_error('Could not establish a connection to MySQL-Database. Check configuration-file!', E_USER_ERROR);
				die();
			}
		
			
			/* Authenticate User */
			/* ================= */
			
			$user = SecurityUser::getUser();
			$user->loadSession();
			
			
			/* Process Asynchronous Request */
			/* ============================ */
			if(isset($_GET['mode']) && $_GET['mode']=="async") {
				
				
				/* Process any System E-Mails, etc. */
				/* ================================ */	
					
				//This is an asynchronous request
				$a = new AsyncHandler();
				$a->handleRequest($_GET, $_POST);
				die();
			}
			
			/* Load Webseite in User-Context */
			/* ============================= */
			
			$fkPage = (isset($_GET['page'])?$_GET['page']:(isset($_POST['page'])?$_POST['page']:""));
			$method = (isset($_GET['method'])?$_GET['method']:(isset($_POST['method'])?$_POST['method']:""));
			
			if(!$fkPage) {
				$fkPage = $this->conf['defaultPage'];
			}
			if(!$method) { $method = "index"; }
			
			$req = new PageRequest($fkPage, $method);
			Runtime::setCurrentRequest($req);
			
		
			try {
			
			$factory = new SiteFactory();
			$factory->addPageRequest($req);
			$factory->loadPage();
			$factory->invokeMethod();
			
			/* Render Webseite */
			/* =============== */
			
			$factory->renderSite();
			$factory->echoSite();
			}
			catch (Exception $e) {
                echo "There was an exception: $e";
				echo "<pre><b>Backtrace</b>\n";
				debug_print_backtrace();
				echo "</pre>";
			}
			//Do something other
				
		}
		
		
	}
?>